package com.tanhua.dubbo.api.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.UserInfo;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void update(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long id) {
        UserInfo userInfo = userInfoMapper.selectById(id);
        return userInfo;
    }

    @Override
    public List<UserInfo> findByIdList(List<Long> userIdList, String gender, Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", userIdList); // id在指定的集合范围内
        queryWrapper.eq("gender", gender); // 性别等于指定的性别
        queryWrapper.ne("id", userId); // id不等于自己的id
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        return userInfoList;
    }

    @Override
    public List<UserInfo> findByIdList(List<Long> userIdList, Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", userIdList); // id在指定的集合范围内
        queryWrapper.ne("id", userId); // id不等于自己的id
        List<UserInfo> userInfoList = userInfoMapper.selectList(queryWrapper);
        return userInfoList;
    }

    @Override
    public IPage<UserInfo> findByPage(Integer page, Integer pagesize) {
        IPage<UserInfo> ipage = new Page<>(page, pagesize);
        IPage<UserInfo> result = userInfoMapper.selectPage(ipage, null);
        return result;
    }

    @Override
    public Integer allUser() {
        Integer count = userInfoMapper.allUserNum();
        return count;
    }

}
